home *** CD-ROM | disk | FTP | other *** search
/ Day Cry / Day Cry CD.bin / oh_towns / taropyon / splib / splib.lzh / PRG / LHX / MATCH.C < prev    next >
C/C++ Source or Header  |  1992-12-08  |  4KB  |  186 lines

  1. /***********************************************************
  2.         match.c -- match filenames
  3. ***********************************************************/
  4. #include    "lh386.h"
  5.  
  6. #include    <stdio.h>
  7. #include    <stdlib.h>
  8. #include    <dos.h>
  9. #include    <string.h>
  10. #include    <ctype.h>
  11. #include    "lh.h"
  12. #include    "errmes.h"
  13.  
  14. #ifdef    __HIGHC__
  15. #    pragma    On(Align_labels);
  16. #endif
  17.  
  18.  
  19. struct pat *pbuf;
  20. static struct pat *pnxt, *plst;
  21.  
  22. /***************************************
  23.         initialize pattern pointers
  24. ***************************************/
  25. void        initpat(void)
  26. {
  27.     pbuf = plst = e_malloc(sizeof(struct pat *));
  28.     pbuf->nxt = NULL;
  29. }
  30.  
  31. /***************************************
  32.         regist file matching pattern
  33. ***************************************/
  34. void        regpat(char *pattern, char *bdir)
  35. {
  36.     char       *p;
  37.  
  38.     p = getfilename(pattern);
  39.     pnxt = e_malloc(strlen(pattern) + sizeof(struct pat));
  40.     pnxt->nxt = NULL;
  41.     pnxt->bdir = bdir;
  42.     pnxt->cnt = 0;
  43.     strcpy(pnxt->pname, pattern);
  44.     pnxt->fname = pnxt->pname + (p - pattern);
  45.     plst = plst->nxt = pnxt;
  46. }
  47.  
  48. /***************************************
  49.         regist base directory
  50. ****************************************
  51.         bdir   : base directory
  52.         return : saved address
  53. ***************************************/
  54. char       *regbdir(char *bdir)
  55. {
  56.     char       *p;
  57.  
  58.     p = e_malloc(strlen(bdir) + 1);
  59.     strcpy(p, bdir);
  60.     return p;
  61. }
  62.  
  63. /***************************************
  64.         match filename to pattern
  65.         including wild cards
  66.  
  67.         pname : filename in the pattern
  68.         fname : filename given
  69. ***************************************/
  70. char       *matchfname(char *pname, char *fname)
  71. {
  72.     char       *f;
  73.  
  74.     f = fname;
  75.     for (; *pname; pname++)
  76.     {
  77.         switch (*pname)
  78.         {
  79.             case '*':
  80.                 while (*fname != '.' &&
  81.                        (uchar) * fname != DELIM && *fname)
  82.                     fname++;
  83.                 break;
  84.             case '?':
  85.                 if (*fname != '.' &&
  86.                     (uchar) * fname != DELIM && *fname)
  87.                     fname++;
  88.                 break;
  89.             case '.':
  90.                 if ((uchar) * fname == DELIM || *fname == '\0')
  91.                     break;
  92.             default:
  93.                 if ((*pname != *fname) &&
  94.                     (flg_i || toupper(*pname) != toupper(*fname)))
  95.                 {
  96.                     return f;    /* not NUL */
  97.                 }
  98.                 fname++;
  99.         }
  100.     }
  101.     return fname;
  102. }
  103.  
  104. /***************************************
  105.         match pathname to pattern
  106. ****************************************
  107.         path   : pathname
  108.         return : it's base directory
  109. ***************************************/
  110. char       *matchpat(char *path)
  111. {
  112.     struct pat *pt;
  113.     char       *p, *r;
  114.     char       *file, *q;
  115.     char        c;
  116.  
  117.     file = getfilename(path);
  118.     for (pt = pbuf->nxt; pt != NULL; pt = pt->nxt)
  119.     {
  120.         p = pt->pname;
  121.         switch (flg_r)
  122.         {
  123.             case 0:
  124.                 if (pt->fname == p && flg_p == 0)
  125.                 {
  126.                     q = matchfname(p, file);
  127.                 } else
  128.                 {
  129.                     q = matchfname(p, path);
  130.                 }
  131.                 if (*q)
  132.                     q = NULL;
  133.                 break;
  134.             case 1:
  135.                 c = *(r = pt->fname);
  136.                 *r = '\0';
  137.                 q = matchfname(p, path);
  138.                 *r = c;
  139.                 if (q)
  140.                 {
  141.                     q = matchfname(pt->fname, file);
  142.                     if (*q)
  143.                         q = NULL;
  144.                 }
  145.                 break;
  146.             case 2:
  147.                 q = matchfname(p, path);
  148.                 if (*q && (uchar) * q != DELIM)
  149.                     q = NULL;
  150.                 break;
  151.         }
  152.         if (q)
  153.             return (pt->bdir);
  154.     }
  155.     return NULL;
  156. }
  157.  
  158. /***************************************
  159.         whether pattern was used or not
  160. ****************************************
  161.         return 0 : all pattern was used
  162.                1 : some pattern not used
  163. ***************************************/
  164. int         tstpat(void)
  165. {
  166.     int         cnt;
  167.     int         errorlevel;
  168.     struct pat *pt;
  169.  
  170.     cnt = 0;
  171.     for (pt = pbuf->nxt; pt != NULL; pt = pt->nxt)
  172.         cnt += pt->cnt;
  173.     if (cnt == 0)                /* no file matched */
  174.         error(NOFILEERR, NULL);
  175.     for (pt = pbuf->nxt; pt != NULL; pt = pt->nxt)
  176.     {
  177.         if (pt->cnt == 0)
  178.         {                        /* if any work name was not used */
  179.             strcpy(work, pt->pname);
  180.             LHX_fprintf(stderr, "%s : '%s'\n", NOMATCHERR, work);
  181.             errorlevel = 1;     /* display warning */
  182.         }
  183.     }
  184.     return errorlevel;
  185. }
  186.